<template>
  <div class="login">

    <div class="left">
      <img class="title_logo" src="../assets/logo/logo_white.png" alt="logo">
    </div>
    <div class="right">
      <!-- <div class="title"></div> -->
      <h1>欢迎登录</h1>
      <div style="text-align: center" class="logo_title">生产执行管理系统</div>
      <!-- <EnvName></EnvName> -->
      <!-- <div>{{ userAgent }}</div> -->
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" @submit.native.prevent>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
            show-password
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <!--        <el-form-item prop="code">-->
        <!--          <el-input-->
        <!--            v-model="loginForm.code"-->
        <!--            auto-complete="off"-->
        <!--            placeholder="请输入验证码"-->
        <!--            style="width: 63%"-->
        <!--            @keyup.enter.native="handleLogin"-->
        <!--          >-->
        <!--            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
        <!--          </el-input>-->
        <!--          <div class="login-code">-->
        <!--            <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
        <!--          </div>-->
        <!--        </el-form-item>-->
        <div class="forget">
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-checkbox v-model="loginForm.grantType" true-label="ad" false-label="password" style="margin:0px 0px 25px 0px;">ldap登陆</el-checkbox>
          <!--          <div class="forget1" :style="{'color': theme}">忘记密码?</div>-->
        </div>

        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;height: 40px"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
      <!-- <div class="bottom">技术支持, 润联智能</div> -->
    </div>

    <!-- 右上角 -->
    <el-dropdown class="right-top" trigger="click">
      <i class="el-icon-setting"></i>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item v-if="device!='desktop'" @click.native="handleSwitchDevice('desktop')">
          <span style="white-space: nowrap;">切换电脑端</span>
        </el-dropdown-item>
        <el-dropdown-item v-else @click.native="handleSwitchDevice('pad')">
          <span style="white-space: nowrap;">切换平板端</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <!--  底部  -->
    <!--    <div class="el-login-footer">-->
    <!--    </div>-->
  </div>
</template>

<script>
  import Aes from "@/utils/crypto.js";
  import { getCodeImg } from "@/api/login";
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from '@/utils/jsencrypt'
  import {mapState, mapGetters} from "vuex";
  // import EnvName from "@/components/EnvName"
  export default {
    // components: { EnvName },
    name: "Login",
    data() {
      return {
        codeUrl: "",
        cookiePassword: "",
        loginForm: {
          username: "",
          password: "",
          rememberMe: false,
          grantType: '',
          code: "",
          uuid: ""
        },
        loginRules: {
          username: [
            { required: true, trigger: "blur", message: "用户名不能为空" }
          ],
          password: [
            { required: true, trigger: "blur", message: "密码不能为空" }
          ],
          code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
        },
        loading: false,
        redirect: undefined,
        // userAgent: ""
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    computed:{
      ...mapGetters([
        'device',
      ]),
      ...mapState({
        theme: state => state.settings.theme
      }),
    },
    created() {
      this.getCode();
      this.getCookie();

      // this.userAgent =`${navigator.userAgent}(${document.body.offsetWidth}x${document.body.offsetHeight})(${screen.width}x${screen.height})`
    },
    methods: {
      getCode() {
        return;
        getCodeImg().then(res => {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        const grantType = Cookies.get('grantType')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
          grantType: grantType === undefined ? '' : grantType
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, { expires: 30 });
              Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
              Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            if (this.loginForm.grantType) {
              Cookies.set("grantType", this.loginForm.grantType, { expires: 30 });
            }
            let key = "";
            let zero = ""
            if(this.loginForm.username.length < 16){
            	let len = this.loginForm.username.length;
            	let num = 16 - Number(len);
            	for(let i=0;i<num;i++){
            		zero += "0"
            	}
            	key = (this.loginForm.username.toLocaleLowerCase())+zero
            }
            else{
            	key = (this.loginForm.username.toLocaleLowerCase()).slice(0,16)
            }
            let loginData= Object.assign({},this.loginForm);
            let password = Aes.encrypt(this.loginForm.password,key);
            loginData.password = password;
            loginData.username = this.loginForm.username.toLocaleLowerCase();
            this.$store.dispatch("Login", loginData).then(() => {
              Cookies.set('news', true)
              this.$router.push({ path: this.redirect || "./" }).catch(()=>{});
              this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
                // if (this.affixTags.some(tag => tag.path === this.$route.path)) {
                //   return
                // }
              })
            }).catch(() => {
              this.loading = false;
              this.getCode();
            });
          }
        });
      },
      handleSwitchDevice(value) {
        localStorage.setItem("device", value);
        location.reload();
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .login {
    display: flex;
    /*justify-content: center;*/
    /*align-items: center;*/
    width: 100%;
    height: 100%;
    /*background-image: url("../assets/images/login-background.jpg");*/
    background-size: 100% 100%;
    background-color: white;
    /*overflow: auto;*/
  }


  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .login-code-img {
    height: 38px;
  }
  .left{
    width: 70%;
    height: 100%;
    background-image: url("../assets/images/logo_bg.png");
    background-size: cover;
    background-position: center 35%;
    background-repeat: no-repeat;
    .title_logo{
      width: 145px;
      margin: 30px 40px;
    }
  }
  .right{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 30%;
    min-width: 440px;
    height: 100%;
    position: relative;
    .title {
      margin: 50px auto 10%;
      text-align: center;
      font-size: 40px;
      font-weight: bold;
      color:rgb(33,158,222);
      letter-spacing:16px;
    }
    .logo_title{
      height: 52px;
      font-size: 36px;
      color: #313131;
      text-align: center;
      font-weight: bold;
      font-style: oblique;
    }
    h1{
      font-weight: 600;
      letter-spacing: 3px;
      text-align: center;
      font-family: cursive;
    }
    .login-form {
      border-radius: 6px;
      width: 360px;
      padding: 25px 25px 5px 25px;
      margin: 10% auto;
      .el-input {
        height: 38px;
        input {
          height: 38px;
        }
      }
      .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
      }
      .forget{
        display: flex;
        justify-content: space-between;
        .forget1{
          cursor: pointer;
          font-size: 14px;
        }
      }
    }
    .bottom{
      width: 100%;
      position: absolute;
      bottom: 30px;
      text-align: center;
      font-size: 12px;
      color:#c9c9c9
    }
  }
  .right-top {
    position: fixed;
    right: 10px;
    top: 10px;
    font-size: 16px;
    cursor: pointer;
  }
</style>
